/* 
 * @Author: zyy
 * @Date:   2015-11-24 15:38:07
 * @Last Modified by:   Marte
 * @Last Modified time: 2015-11-25 21:09:50
 */

$(document).ready(function() {

    var test_psd,
        test_spsd;

    // 判断邮箱格式
    function isEmail(str) {
        var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.([a-zA-Z0-9_-])+)+/;
        return reg.test(str);
    }
    // 判断密码格式
    function isPsd(str) {
        var reg = /^[a-zA-Z0-9_-]+/;
        return reg.test(str);
    }

    // 正确提示与错误提示
    function rightTip(id) {
        $(id).parent().parent()
            .children(".u-input-tip").children('label').children('i')
            .removeClass('icon-errortip').addClass('icon-righttip');
        $(id).parent().parent()
            .children(".u-input-tip").children('label').children('span')
            .text(" ");
    }

    function errorTip(id, tipText) {

        $(id).parent().parent()
            .children(".u-input-tip").children('label').children('i')
            .removeClass('icon-righttip').addClass('icon-errortip');
        $(id).parent().parent()
            .children(".u-input-tip").children('label').children('span')
            .text(tipText);
    }

    $("#reg-psd").bind("blur", function() {

        var psd = $("#reg-psd").val();
        // console.log(psd.length);
        if (isPsd(psd) && psd.length >= 6) {
            rightTip("#reg-psd", '');
            test_psd = true;
        } else {
            errorTip("#reg-psd", '密码格式不正确');
            test_psd = false;
        }
        disableButton();
    });
    $("#reg-surepsd").bind("blur", function() {

        var psd = $("#reg-psd").val();
        var surepsd = $("#reg-surepsd").val();
        if (surepsd === psd && surepsd.length >= 6 && surepsd.length <= 20) {
            rightTip("#reg-surepsd", '');
            test_spsd = true;
        } else {
            errorTip("#reg-surepsd", '密码不一致');
            test_spsd = false;
        }
        disableButton();
    });

    // 注册按钮解禁
    function disableButton() {
        if (test_psd && test_spsd) {
            $('.u-registButton').removeAttr('disabled');
        } else {
            $('.u-registButton').attr('disabled', 'disabled');
        }
    }

    // 注册接口
    function dealRegister(username, psd) {
        var url = "api_register";
        $.post(
            url, {
                login_name: username,
                password: psd
            },
            function(data) {
                if (data.state == 200) {
                    window.location.href = data.goPage;
                } else {
                    alert(data.message);
                }
            }, "json"
        );
    }

    $(".u-registButton").on("click", function() {
        var email = $("#reg-Email").val();
        var psd = $("#reg-psd").val();
        dealRegister(email, psd);
    });
    // 登陆提示
    function logTip(className, text, state) {
        if (state == 'right') {
            $('.u-input-tip i').eq(className)
                .removeClass('icon-errortip');
            $('.u-tip-word').eq(className)
                .text(" ");
        } else {
            $('.u-input-tip i').eq(className)
                .removeClass('icon-righttip');

            $('.u-tip-word').eq(className)
                .text(text);
        }
    }
    $("#log_mail").on("focus", function() {
        logTip(0, "", "right");
    })

    $('#log_psd').on("focus", function() {

            logTip(1, "", "right");

        })
        // 登陆接口
    $('.u-loginButton').click(function(event) {
        var mail = $("#log_mail").val();
        var psd = $('#log_psd').val();
        var url = "api_login";
        if (!mail) {
            logTip(0, "输入为空", "error");
            return;
        } else if (!psd) {
            logTip(1, "输入为空", "error");
            return;
        }
        logTip(0, "", "right");
        logTip(1, "", "right");
        $.post(
            url, {
                login_name: mail,
                password: psd
            },
            function(data) {
                if (data.state == 200) {
                    window.location.href = data.goPage;
                } else {
                    alert(data.message);
                }
            }, "json");
    });




});
